using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ExpertSMS.Models;

namespace ExpertSMS.Controllers
{   
    public class BillOfLadingsController : Controller
    {
        private ExpertSMSContext context = new ExpertSMSContext();

        //
        // GET: /BillOfLadings/

        public ViewResult Index()
        {
            return View(context.BillOfLadings.Include(billoflading => billoflading.Supplier).Include(billoflading => billoflading.Store).Include(billoflading => billoflading.BillOfLadingProducts).ToList());
        }

        public ActionResult Confirm(int id)
        {
            BillOfLading billOfLading = context.BillOfLadings.Single(x => x.BillOfLadingID == id);
            billOfLading.IsConfirmed = true;
            context.SaveChanges();
            foreach (var item in billOfLading.BillOfLadingProducts)
            {
                StoreProductStock storeProductStock = new StoreProductStock
                {
                    CurrentQuantity = item.Quantity,
                    Date = item.BillOfLading.Date,
                    OriginalQuantity = item.Quantity,
                    ProductID = item.ProductID,
                    StoreID = item.BillOfLading.StoreID,
                    UnitID = item.UnitID,
                    Cost = item.Cost
                };
                context.StoreProductStocks.Add(storeProductStock);
            }
            context.SaveChanges();
            return RedirectToAction("Details", new { id = id });
        }
        public ViewResult Details(int id)
        {
            BillOfLading billoflading = context.BillOfLadings.Single(x => x.BillOfLadingID == id);
            return View(billoflading);
        }

        //
        // GET: /BillOfLadings/Create

        public ActionResult Create(int supplierID)
        {
            ViewBag.Supplier = context.Suppliers.Single(x => x.SupplierID == supplierID);
            ViewBag.PossibleStores = context.Stores;
            return View();
        } 

        //
        // POST: /BillOfLadings/Create

        [HttpPost]
        public ActionResult Create(BillOfLading billoflading)
        {
            if (ModelState.IsValid)
            {
                context.BillOfLadings.Add(billoflading);
                context.SaveChanges();
                return RedirectToAction("Details", new { id = billoflading.BillOfLadingID });  
            }

            ViewBag.PossibleSuppliers = context.Suppliers;
            ViewBag.PossibleStores = context.Stores;
            return View(billoflading);
        }
        
        //
        // GET: /BillOfLadings/Edit/5
 
        public ActionResult Edit(int id)
        {
            BillOfLading billoflading = context.BillOfLadings.Single(x => x.BillOfLadingID == id);
            ViewBag.PossibleSuppliers = context.Suppliers;
            ViewBag.PossibleStores = context.Stores;
            return View(billoflading);
        }

        //
        // POST: /BillOfLadings/Edit/5

        [HttpPost]
        public ActionResult Edit(BillOfLading billoflading)
        {
            if (ModelState.IsValid)
            {
                context.Entry(billoflading).State = EntityState.Modified;
                context.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.PossibleSuppliers = context.Suppliers;
            ViewBag.PossibleStores = context.Stores;
            return View(billoflading);
        }

        //
        // GET: /BillOfLadings/Delete/5
 
        public ActionResult Delete(int id)
        {
            BillOfLading billoflading = context.BillOfLadings.Single(x => x.BillOfLadingID == id);
            return View(billoflading);
        }

        //
        // POST: /BillOfLadings/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            BillOfLading billoflading = context.BillOfLadings.Single(x => x.BillOfLadingID == id);
            context.BillOfLadings.Remove(billoflading);
            context.SaveChanges();
            return RedirectToAction("Index");
        }
    }
}